package com.dlc.toolkit.widget

import android.animation.ValueAnimator
import android.content.Context
import android.util.ArraySet
import android.util.AttributeSet
import android.util.Log
import android.view.*
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.core.view.iterator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.dlc.toolkit.R
import kotlin.math.abs

class DLCStockScrollView<T> : LinearLayout {

    private var mScrollHeaderContainer: LinearLayout? = null
    private var mScrollContainerViews: ArraySet<View> = ArraySet()
    var tvHeader: TextView? = null


    private var isAnimate = false
    private var animator: ValueAnimator? = null
    private var mStartX = 0f
    private var mTouchSlop = 0 //move事件最小阈值
    private var mFixX = 0//已滑动的距离
    private var mMoveOffsetX = 0
    private var mMovableTotalWidth = 0


    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )


    fun initView(context: Context, data: ArrayList<T>) {
        var inflater: View =
            LayoutInflater.from(context).inflate(R.layout.widget_stock_scroll, this, true)

        val configuration = ViewConfiguration.get(context)
        mTouchSlop = configuration.scaledTouchSlop

        var header: LinearLayout = inflater.findViewById(R.id.header)
        mScrollHeaderContainer = header.findViewById(R.id.llContainer)
        tvHeader = header.findViewById(R.id.tvFixCol)
        var rvList: RecyclerView = inflater.findViewById(R.id.rv_list)

        var layoutManager = LinearLayoutManager(context)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        rvList.layoutManager = layoutManager
        rvList.adapter = DLCStockScrollViewAdapter(data, R.layout.item_dlc)
    }


    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        when (ev?.action) {
            MotionEvent.ACTION_DOWN -> {
                if (isAnimate && animator != null) animator?.cancel()
                mStartX = ev.x
            }
            MotionEvent.ACTION_MOVE -> {
                val distanceX: Float = ev.x - mStartX
                parent.requestDisallowInterceptTouchEvent(true)
                return abs(distanceX) > mTouchSlop
            }
            MotionEvent.ACTION_UP -> {}
        }
        return super.onInterceptTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                mStartX = event.x
                return true
            }
            MotionEvent.ACTION_MOVE -> {
                val offsetX = abs(event.x - mStartX).toInt()
                if (offsetX > mTouchSlop) {
                    mMoveOffsetX = (mStartX - event.x + mFixX).toInt()
                    //Log.d("TAGGGGGG", "Linearlayout滑动的距离原生=++++++++++++++>${mStartX - event.x} ")
                    if (mStartX - event.x <= 0) {//说明向左
                        if (mMoveOffsetX <= 0) {//已经最左
                            mMoveOffsetX = 0
                            mFixX = 0
                        }
                    } else {
                        if (mFixX + mMoveOffsetX >= (movableTotalWidth() - (context.resources.displayMetrics.widthPixels - tvHeader!!.width))) {
                            mMoveOffsetX =
                                (movableTotalWidth() - (context.resources.displayMetrics.widthPixels - tvHeader!!.width))
                        }
                    }
                    //Log.d("TAGGGGGG", "mFixX=>$mFixX ")
                    //Log.d("TAGGGGGG", "手指=>$offsetX ")
                    //Log.d("TAGGGGGG", "可滑动总宽=>" + movableTotalWidth() + " ")
                    Log.d("TAGGGGGG", "Linearlayout滑动的距离=>$mMoveOffsetX ")
                    mScrollHeaderContainer?.scrollTo(mMoveOffsetX, 0)
                    scrollTo(mMoveOffsetX)

                }
            }
            MotionEvent.ACTION_UP -> {
                mFixX = mMoveOffsetX
            }
        }
        return super.onTouchEvent(event)
    }

    private fun movableTotalWidth(): Int {
        if (mMovableTotalWidth != 0) return mMovableTotalWidth
        for (view in mScrollHeaderContainer!!) {
            mMovableTotalWidth += view.width
        }
        return mMovableTotalWidth
    }


    private fun scrollTo(x: Int) {
        for (view in mScrollContainerViews) {
            view.scrollTo(x, 0)
        }
    }

    fun actionUP(){

    }


    inner class DLCStockScrollViewAdapter<T>(
        private var data: ArrayList<T>,
        @LayoutRes var layoutId: Int
    ) :
        RecyclerView.Adapter<DLCStockScrollViewAdapterViewHolder>() {
        //设置每个子项的长相
        override fun onCreateViewHolder(
            parent: ViewGroup,
            viewType: Int
        ): DLCStockScrollViewAdapterViewHolder {
            var view: View = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
            return DLCStockScrollViewAdapterViewHolder(view)

        }

        //每个子项的数据
        override fun onBindViewHolder(holder: DLCStockScrollViewAdapterViewHolder, position: Int) {
            var scrollView: View = holder.itemView.findViewById(R.id.llContainer)
            mScrollContainerViews.add(scrollView)
        }

        override fun getItemCount(): Int = data.size

    }

    inner class DLCStockScrollViewAdapterViewHolder(itemView: View) :
        RecyclerView.ViewHolder(itemView) {

    }

}

